<template>
  <div class="box">
    <div class="login">
      <h2>叩丁狼商品管理系统</h2>
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        status-icon
        :rules="rules"
        class="demo-ruleForm"
      >
        <el-form-item prop="username">
          <el-input v-model="ruleForm.username" placeholder="请输入用户名">
            <template #prefix>
              <el-icon :size="20" class="el-input__icon"
                ><User
              /></el-icon> </template
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="ruleForm.password"
            type="password"
            autocomplete="off"
          >
            <template #prefix>
              <el-icon :size="20" class="el-input__icon"
                ><Lock
              /></el-icon> </template
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm(ruleFormRef)"
            >提交</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import type { FormInstance } from "element-plus";
import { User, Lock } from "@element-plus/icons-vue";
import { ElMessage } from "element-plus";
import { loginApi } from "../../request/api";
import { useRouter } from "vue-router";
// import router from "../../router/index";
const router = useRouter();
const ruleFormRef = ref<FormInstance>();

const checkAge = (rule: any, value: any, callback: any) => {
  if (!value) {
    return callback(new Error("Please input the age"));
  }
  setTimeout(() => {
    if (!Number.isInteger(value)) {
      callback(new Error("Please input digits"));
    } else {
      if (value < 18) {
        callback(new Error("Age must be greater than 18"));
      } else {
        callback();
      }
    }
  }, 1000);
};

const validatePass = (rule: any, value: any, callback: any) => {
  if (value === "") {
    callback(new Error("请输入账号"));
  } else {
    if (ruleForm.password !== "") {
      if (!ruleFormRef.value) return;
      ruleFormRef.value.validateField("checkPass", () => null);
    }
    callback();
  }
};
const validatePass2 = (rule: any, value: any, callback: any) => {
  if (value === "") {
    callback(new Error("请输入密码"));
  } else {
    callback();
  }
};

const ruleForm = reactive({
  username: "",
  password: "",
});

const rules = reactive({
  username: [{ required: true, message: "请输入账号", trigger: "blur" }],
  checkPass: [{ validator: validatePass2, trigger: "blur" }],
  age: [{ validator: checkAge, trigger: "blur" }],
});

const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.validate((valid) => {
    if (valid) {
      console.log("submit!");
      loginApi(ruleForm).then((res) => {
        console.log(res.data.data);

        if (res.data.code === 200) {
          localStorage.setItem(
            "token",
            res.data.data.tokenHead + res.data.data.token
          );
          ElMessage.success("登陆成功");
          console.log(123123123);
          router.push("/home");
          // router.push("/home");
        }
      });
    } else {
      console.log("error submit!");
      ElMessage({
        message: "Warning, this is a warning message.",
        type: "warning",
      });
      return false;
    }
  });
};

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};
</script>

<style scoped lang="less">
.box {
  width: 100%;
  height: 100vh;
  background-color: #304156;
  display: flex;
  justify-content: center;
  align-items: center;
  .login {
    width: 400px;
    text-align: center;
    h2 {
      color: white;
    }
    :deep(.el-button--primary) {
      width: 100%;
    }
    :deep(.el-input__inner) {
      height: 50px;
    }
  }
}
</style>
